package com.ysh.webflux.controller;

import com.sun.xml.internal.ws.binding.FeatureListUtil;
import com.ysh.webflux.pojo.User;
import com.ysh.webflux.service.UserService;
import javafx.scene.media.Media;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.awt.*;

/**
 * 袁胜虎 on 2022/9/22 16:24
 */
@RestController
public class TestController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @GetMapping("/user/info/{id}")
    public Mono<User> findById(@PathVariable int id){
        return userService.getUserById(id);
    }
    @GetMapping("/users")
    public Flux<User> findAllUser(){
        return userService.getAllUser();
    }
    @PostMapping("/user")
    public Mono<Void> addUser(@RequestBody User user){
        return userService.saveUserInfo(Mono.just(user));
    }
    /**
     * produces 可以设置响应结果的类型
     * @return
     */
    @GetMapping(value = "/user/stream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<User> getUserStream(){
        return userService.getUsersStream();
    }

    @GetMapping("/user1/{id}")
    public Mono<User> findById2(@PathVariable int id){
        User user = userService.getUserById1(id);
        return Mono.just(user);
    }
    @PostMapping("/user1")
    public Mono<Void> addUser2(@RequestBody User user){
        userService.saveUserInfo1(user);
        return Mono.empty();
    }
}
